package com.ruoyi.wms.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.wms.dto.DisassembleTaskDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.wms.domain.DisassembleTask;
import com.ruoyi.wms.service.IDisassembleTaskService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 拆解任务Controller
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@Api(value = "pda拆解作业也要用。拆解具体车辆的工序即任务Controller", tags = "拆解任务Controller")
@RestController
@RequestMapping("/disassembleTask")
public class DisassembleTaskController extends BaseController {
    @Autowired
    private IDisassembleTaskService disassembleTaskService;

    /**
     * 查询拆解任务列表
     */
    @RequiresPermissions("wms:task:list")
    @GetMapping("/list")
    public TableDataInfo list(DisassembleTaskDto disassembleTask) {
        startPage();
        List<DisassembleTask> list = disassembleTaskService.selectDisassembleTaskList(disassembleTask);
        return getDataTable(list);
    }


    /**
     * 获取拆解任务详细信息
     */
    @RequiresPermissions("wms:task:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(disassembleTaskService.selectDisassembleTaskById(id));
    }


    /**
     * 修改拆解任务
     */
    @RequiresPermissions("wms:task:edit")
    @Log(title = "拆解任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DisassembleTask disassembleTask) {
        return toAjax(disassembleTaskService.updateDisassembleTask(disassembleTask));
    }

    /**
     * 删除拆解任务
     */


    @ApiOperation("拆解任务")
    @Log(title = "拆解任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody String[] ids) {
        if (ids == null || ids.length == 0) {
            return error("未选中");
        }
        return toAjax(disassembleTaskService.deleteDisassembleTaskByIds(ids));
    }

    @ApiOperation("拆解作业，扫码-根据车牌号码+拆解员+工序 查任务")
    @GetMapping(value = "/getInfoByplateNo")
    public AjaxResult getInfoByplateNo(@ApiParam(value = "车牌号码", required = true)
                                       @RequestParam(value = "plateNo", required = true) String plateNo,

                                       @ApiParam(value = "拆解人员", required = true)
                                       @RequestParam(value = "dismantler", required = true) String dismantler,

                                       @ApiParam(value = "工序名称", required = true)
                                       @RequestParam(value = "processName", required = true) String processName) {
        DisassembleTaskDto dto = new DisassembleTaskDto();
        dto.setDismantler(dismantler);
        dto.setPlateNo(plateNo);
        dto.setProcessName(processName);
        List<DisassembleTask> list = disassembleTaskService.selectDisassembleTaskList(dto);
        return success(list);
    }

    @ApiOperation("生成任务，根据管理员就是任务下发人 生成25个任务")
    @PostMapping("/assignTask")
    public AjaxResult assignTask(@ApiParam(value = "车牌号码", required = true)
                                     @RequestParam(value = "plateNo", required = true) String plateNo,@ApiParam(value = "车牌号码", required = true)
    @RequestParam(value = "taskIssuer", required = true) String taskIssuer ) {

        DisassembleTask task = new DisassembleTask();
        task.setPlateNo(plateNo );
        task.setTaskIssuer( taskIssuer);
        String msg = "";
        try{
            msg = disassembleTaskService.assignTask(task) ;
            return success(msg);
        }catch (Exception e){
            e.printStackTrace();
            return  error("执行错误,"+e.getMessage()  );
        }


    }




}
